import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Point;

/**
 * @author qingkangxu
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class TabFolderTest {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display, SWT.SHELL_TRIM);
		shell.setText("TabFolder");
		shell.setLayout(new FillLayout());

		TabFolder folder = new TabFolder(shell, SWT.NONE);
		TabItem item1 = new TabItem(folder, SWT.NONE);
		item1.setText("label");

		// create the sc
		final ScrolledComposite sc = new ScrolledComposite(folder, SWT.V_SCROLL);
		sc.setExpandHorizontal(true);
		sc.setExpandVertical(true);
		sc.getVerticalBar().setIncrement(5);

		// create the composite on the sc
		final Composite endpointComp = new Composite(sc, SWT.NONE);
		GridData suGridData = new GridData(GridData.FILL_BOTH);
		endpointComp.setLayoutData(suGridData);
		endpointComp.setLayout(new GridLayout(1, true));
		// add button to the endpointComp
		addCompToEndpointComp(endpointComp);

		sc.addControlListener(new ControlAdapter() {
			public void controlResized(ControlEvent e) {
				Point p = endpointComp.computeSize(SWT.DEFAULT, SWT.DEFAULT);
				sc.setMinSize(p.x, p.y + 10);
				endpointComp.layout();
			}
		});
		sc.setContent(endpointComp);
		item1.setControl(sc);

		TabItem item2 = new TabItem(folder, SWT.NONE);
		item2.setText("radio");
		Group group2 = new Group(folder, SWT.NONE);
		group2.setText("Radio Group");
		group2.setLayout(new GridLayout());
		Button radio1 = new Button(group2, SWT.RADIO);
		radio1.setText("radio1");
		Button radio2 = new Button(group2, SWT.RADIO);
		radio2.setText("radio2");
		item2.setControl(group2);

		shell.setVisible(true);
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private static void addCompToEndpointComp(Composite endpointComp) {
		for (int i = 0; i < 20; i++) {
			Button button = new Button(endpointComp, SWT.PUSH);
			button.setText("ForTestBtn" + i);
			button.setLayoutData(new GridData());
		}

	}
}
